Benutzer:Kybot/movebot.py
Beschreibung: Dient zum Verschieben vom ANR in den HNR. Gleichzeitig werden alle relevanten Seiten im HNR angelegt. Beispieloutput: hnr.txt # -*- coding: utf-8 -*- #!/usr/bin/python #movebot.py -titleregex:"^Foo" -pubyear:9999 -startpage:12 -endpage:355 -bcstartpage:-10 -bcendpage:390 -target:Foo #movepages.py -noredirect -pairs:_m.txt #pagefromfile.py -file:_m_main.txt -notitle -autosummary from __future__ import division """ The following parameters are supported: &params; -dry If given, doesn't do any real changes, but only shows what would have been changed. All other parameters will be regarded as part of the title of a single page, and the bot will only work on that single page. """ __version__ = '$Id$' import wikipedia as pywikibot import pagegenerators import sys import codecs # This is required for the text that is shown when you run this script # with the parameter -help. docuReplacements = { '&params;': pagegenerators.parameterHelp } class Config(object): def __init__(self): self.namespace = 112 self.target = 'Foo' self.startpage = None self.endpage = None self.bcstartpage = None self.bcendpage = None self.pubyear = None config = Config() _unpatched = pywikibot.Site.allpages def patch_allpages(self, start='!', namespace=None, includeredirects=True, throttle=True): return _unpatched(self, start=start, namespace=config.namespace, includeredirects=includeredirects, throttle=throttle) pywikibot.Site.allpages = patch_allpages class BasicBot: def __init__(self, generator, dry): """ Constructor. Parameters: * generator - The page generator that determines on which pages to work on. """ self.generator = generator def run(self): #get the text file for moving ofile = open('_m.txt', 'w') for page in self.generator: self.treat(page, ofile) ofile.close() #generate the main page template ofile = open('_m_main.txt', 'w') ofile.write(' \n') ofile.write("" + config.target + "\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n') ofile.write(' Seiten \n') ofile.write(' \n') ofile.write(' \n') ofile.write('::Seite ' + str(config.startpage) + '-' + str(config.endpage) + ': Haupttext\n') ofile.write((u"::Blau gefärbte Seitenzahlen führen zur Dokumentation der dort gefundenen Textübernahmen '(Stand : von " + str(config.endpage-config.startpage+1) + " Seiten, /" + str((config.endpage-config.startpage+1)/100) + " round 2}}%, Seiten mit 50%-75% Plagiatstext]], Seiten mit mehr als 75% Plagiatstext]])'' \n").encode("utf-8")) ofile.write("::Grau dargestellt sind Seiten ohne Fundstellen oder solche, die noch nicht geprüft und aufbereitet wurden.\n") ofile.write('\n') ofile.write(' Wichtige Seiten \n') ofile.write('*Befunde\n') ofile.write('*Alle Fragmente\n') ofile.write('*Alle Quellen ( )]]\n') ofile.write(' \n') ofile.write(' \n') #generate the title record page ofile.write('\n \n') ofile.write("" + config.target + "/Titelaufnahme'\n") ofile.write(" 'Eine kritische Auseinandersetzung mit der Dissertation von Dr. " + config.target + " \n") ofile.write(' \n') ofile.write(' \n') ofile.write(' \n') #generate the barcode parameter page ofile.write('\n \n') ofile.write("" + config.target + "/AutoBarcodeParameter'\n") ofile.write('{ "AutoBarcodeParameter": {\n') ofile.write('\t"prefix": "' + config.target + '",\n') ofile.write('\t"reference": "+ config.target ) if config.pubyear != None: ofile.write(' ' + str(config.pubyear)) ofile.write('",\n') ofile.write('\t"from": ' + str(config.bcstartpage) + ',\n') ofile.write('\t"to": ' + str(config.bcendpage) + ',\n') ofile.write('\t"range": {"from": ' + str(config.startpage) + ', "to": ' + str(config.endpage) + '}\n') ofile.write('\t}\n') ofile.write('}\n') ofile.write(' \n') #generate the qs pages ofile.write('\n \n') ofile.write("'Quelle:" + config.target + "\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("" + config.target + "/Alle_Fragmente'\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'" + config.target + "/Fragmente/Verwaist_n'\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'" + config.target + "/Fragmente/Sichtung_p'\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'" + config.target + "/Fragmente/Sichtung_v'\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'" + config.target + "/Fragmente/Sichtung_k'\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'" + config.target + "/Fragmente/Sichtung_u'\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'" + config.target + "/Fragmente/Gesichtet_u'\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'" + config.target + "/Fragmente/Gesichtet_g'\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'" + config.target + "/Fragmente/Gesichtet_s'\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'" + config.target + "/Fragmente/Gesichtet_v'\n") ofile.write(' \n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'" + config.target + "/Fragmente/Gesichtet_k'\n") ofile.write(' \n') ofile.write(' \n') #generate the concept pages ofile.write('\n \n') ofile.write("'Konzept:" + config.target + "_und_Gt50'\n") ofile.write(' \n') ofile.write('Kategorie:Konzepte\n') ofile.write(' \n') ofile.write('\n \n') ofile.write("'Konzept:" + config.target + "_und_Gt75'\n") ofile.write(' \n') ofile.write('Kategorie:Konzepte\n') ofile.write(' \n') #generate the discussion page ofile.write('\n \n') ofile.write("'" + config.target + "/Diskussionsmonitor'\n") ofile.write(' \n') ofile.write(' \n') #generate the template pages ofile.write('\n \n') ofile.write("'Vorlage:PlagPage" + config.target + "'''\n") ofile.write(' \n') ofile.write('PlagPage' + config.target + '\n') ofile.write(' \n') def treat(self, page, ofile): """ Loads the given page """ initials = '' try: # Load the page text = page.get() exportText = '' + page.title() + '' + '\n' + ''+ page.titleWithoutNamespace() + '' + '\n' #exportText = '' + page.title() + '' + '\n' + 'Analyse:'+ page.title() + '' + '\n' #exportText = '' + page.title() + '' + '\n' + '[initials + '/' + page.titleWithoutNamespace().split('/')[1 + ']]' + '\n' #--> Benutzer_Diskussion -> Analyse_Diskussion ebenfalls verschieben ofile.write(exportText) except pywikibot.NoPage: pywikibot.output(u"Page %s does not exist; skipping." % page.aslink()) return except pywikibot.IsRedirectPage: pywikibot.output(u"Page %s is a redirect; skipping." % page.aslink()) return def main(): # This factory is responsible for processing command line arguments # that are also used by other scripts and that determine on which pages # to work on. genFactory = pagegenerators.GeneratorFactory() # The generator gives the pages that should be worked upon. gen = None # This temporary array is used to read the page title if one single # page to work on is specified by the arguments. pageTitleParts = [] # If dry is True, doesn't do any real changes, but only show # what would have been changed. dry = False # Parse command line arguments for arg in pywikibot.handleArgs(): # check if a standard argument like # -start:XYZ or -ref:Asdf was given. if arg.startswith("-target:"): config.target = arglen('-target:'): elif arg.startswith("-startpage:"): config.startpage = int(arglen('-startpage:'):) elif arg.startswith("-endpage:"): config.endpage = int(arglen('-endpage:'):) elif arg.startswith("-bcstartpage:"): config.bcstartpage = int(arglen('-bcstartpage:'):) elif arg.startswith("-bcendpage:"): config.bcendpage = int(arglen('-bcendpage:'):) elif arg.startswith("-pubyear:"): config.pubyear = int(arglen('-pubyear:'):) elif not genFactory.handleArg(arg): pageTitleParts.append(arg) if pageTitleParts != []: # We will only work on a single page. pageTitle = ' '.join(pageTitleParts) page = pywikibot.Page(pywikibot.getSite(), pageTitle) gen = iter(page) if not gen: gen = genFactory.getCombinedGenerator() if gen: # The preloading generator is responsible for downloading multiple # pages from the wiki simultaneously. gen = pagegenerators.PreloadingGenerator(gen) bot = BasicBot(gen, dry) bot.run() else: pywikibot.showHelp() if __name__ "__main__": try: main() finally: pywikibot.stopme()